import FetchHelper from "../helper/fetch-helper";
import _ from 'lodash';
import { isIntersects, wktToGeom } from "../helper/mapHelper";

export default class JcdjcApi{
    static async listByPolygon(polygon){
        const resp = await FetchHelper.getJson(`/admin-api/ybyj/jcd/list`)
        const datas = resp.rows?.filter(item => {
            if (!item.geom) {
                return false;
            }
            const geom = wktToGeom(item.geom);
            return isIntersects(geom, polygon)
        })
        const groupData = _.groupBy(datas || [], 'cztxl');
        return {
            data: _.keys(groupData).map(key => {
                return {
                    name: key,
                    count: groupData[key].length,
                    list: groupData[key]
                }
            })
        };
    }

    static async list(params){
        try{
            const resp=await FetchHelper.getJson(`/admin-api/ybyj/jcd/list`,{
                ...params
            })
            resp.rows=resp?.rows?.map(item=>{
                if(item?.xq){
                    try{
                        const json=JSON.parse(item?.xq);
                        return{
                            ...json,
                            ...item,
                        }
                    }catch(e){

                    }
                }
                return item;
            })
            return resp;
        }catch(err){
            return {rows:[]}
        }
    }
}